/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Cari.java
 *
 * Created on Dec 4, 2010, 10:03:00 PM
 */

package stta.elinfo.perpustakaan.gui;

import java.awt.Paint;
import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.table.DefaultTableModel;
import stta.elinfo.perpustakaan.dao.entity.Buku;
import stta.elinfo.perpustakaan.dao.entity.Kategori;
import stta.elinfo.perpustakaan.dao.impl.BukuImpl;
import stta.elinfo.perpustakaan.dao.impl.KategoriImpl;
import stta.elinfo.perpustakaan.dao.interf.BukuInterf;
import stta.elinfo.perpustakaan.dao.interf.KategoriInterf;

/**
 *
 * @author fendi
 */
public class Cari extends javax.swing.JPanel {
    private Paint paint;

    KategoriInterf kategori;
    BukuInterf buku;
    List<Buku>list=new ArrayList<Buku>();
    String dir,tujuan;
    /** Creates new form Cari */
    public Cari() {
        initComponents();
        buku=new BukuImpl();
        kategori=new KategoriImpl();
        dir=System.getProperty("user.dir");
        try {
            loadKategori();
        } catch (SQLException ex) {
            Logger.getLogger(Cari.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            list = this.buku.getAll();
        } catch (SQLException ex) {
            Logger.getLogger(Cari.class.getName()).log(Level.SEVERE, null, ex);
        }
        isiTable();
    }

    void loadKategori() throws SQLException{
        combokategori.removeAllItems();
        for(Kategori kat : kategori.getAll()){
            combokategori.addItem(kat.getKategori());
        }
    }

    void isiTable(){
    Object data[][]=new Object[list.size()][4];
    int x=0;
    for(Buku bk : list){
        data[x][0]=bk.getNama();
        data[x][1]=bk.getKategori();
        data[x][2]=bk.getDeskripsi();
        data[x][3]=bk.getTanggal();
        x++;
    }
    String[] judul={"Nama","Kategori","Deskripsi","Tanggal"};
    tabel.setModel(new DefaultTableModel(data, judul));
    jScrollPane1.setViewportView(tabel);
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        combokategori = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        txtnama = new stta.elinfo.perpustakaan.template.TextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        tabel = new javax.swing.JTable();
        button1 = new stta.elinfo.perpustakaan.template.Button();
        jLabel3 = new javax.swing.JLabel();

        setOpaque(false);

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 12));
        jLabel1.setForeground(new java.awt.Color(255, 255, 255));
        jLabel1.setText("Kategori");

        combokategori.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        combokategori.setLightWeightPopupEnabled(false);
        combokategori.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                combokategoriActionPerformed(evt);
            }
        });

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 12));
        jLabel2.setForeground(new java.awt.Color(255, 255, 255));
        jLabel2.setText("Nama Buku");

        txtnama.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtnamaActionPerformed(evt);
            }
        });
        txtnama.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtnamaKeyPressed(evt);
            }
        });

        tabel.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(tabel);

        button1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/stta/elinfo/perpustakaan/gambar/open.png"))); // NOI18N
        button1.setText("open");
        button1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                button1ActionPerformed(evt);
            }
        });

        jLabel3.setFont(new java.awt.Font("Comic Sans MS", 3, 18));
        jLabel3.setForeground(new java.awt.Color(255, 255, 255));
        jLabel3.setText("Cari File");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGap(430, 430, 430))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 485, Short.MAX_VALUE)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(layout.createSequentialGroup()
                                    .addGap(332, 332, 332)
                                    .addComponent(button1, javax.swing.GroupLayout.PREFERRED_SIZE, 140, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGroup(layout.createSequentialGroup()
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(jLabel2)
                                        .addComponent(jLabel1))
                                    .addGap(18, 18, 18)
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                        .addComponent(txtnama, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(combokategori, javax.swing.GroupLayout.PREFERRED_SIZE, 147, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                        .addGap(19, 19, 19))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(60, Short.MAX_VALUE)
                .addComponent(jLabel3)
                .addGap(31, 31, 31)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtnama, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(combokategori, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(button1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void txtnamaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtnamaActionPerformed

    }//GEN-LAST:event_txtnamaActionPerformed

    private void txtnamaKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtnamaKeyPressed
        try {
            // TODO add your handling code here:
            this.list = this.buku.getKatNama(txtnama.getText(), (String) combokategori.getSelectedItem());
            isiTable();
        } catch (SQLException ex) {
            Logger.getLogger(Cari.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_txtnamaKeyPressed

    private void combokategoriActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_combokategoriActionPerformed
        try {
            // TODO add your handling code here:
            this.list = this.buku.getKatNama(txtnama.getText(), (String) combokategori.getSelectedItem());
            isiTable();
        } catch (SQLException ex) {
            Logger.getLogger(Cari.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_combokategoriActionPerformed

    private void button1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_button1ActionPerformed
        int row=tabel.getSelectedRow();
        if(row!=-1){
           try {
            // TODO add your handling code here:

            tujuan = dir.substring(0, dir.length() - 5)+list.get(row).getPath();
//            File buka=new File(tujuan);
            Process p = Runtime.getRuntime().exec(new String[]{"rundll32","url.dll,FileProtocolHandler", tujuan});
            p.waitFor();
//            cmds.add(String.format("gnome-open %s", tujuan));
//     String subCmd = (exec) ? "exec" : "openURL";
//     cmds.add(String.format("kfmclient "+subCmd+" %s", fileName));
//            Runtime r = Runtime.getRuntime();
//    String myScript =tujuan;
//    String[] cmdArray = {"gnome-open", "-e", myScript + " ; le_exec"};
//    r.exec(cmdArray).waitFor();
//            String command= tujuan;
//    Runtime rt = Runtime.getRuntime();
//    Process pr = rt.exec(command);

        } catch (InterruptedException ex) {
            Logger.getLogger(Cari.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Cari.class.getName()).log(Level.SEVERE, null, ex);
        } 
        }
        
    }//GEN-LAST:event_button1ActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private stta.elinfo.perpustakaan.template.Button button1;
    private javax.swing.JComboBox combokategori;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tabel;
    private stta.elinfo.perpustakaan.template.TextField txtnama;
    // End of variables declaration//GEN-END:variables

}
